<template>
  <div class="shareArt" @click="toArt">
    <h3 v-html="item.title"></h3>
    <p>{{ item.content }}</p>
    <div class="info" v-if="item.author">
      <img :src="baseUrl + item.author.avatar" alt="" />
      <span class="uname">{{ item.author.nickname }}</span>
      <span class="time">{{ item.created_at | formatTime }}</span>
      <span class="read"><van-icon name="eye-o" /> {{ item.read }}</span>
      <span
        ><van-icon
          name="good-job-o"
          @click.stop="zan(item.id)"
          :class="iszan ? '' : 'red'"
        />
        {{ zannum }}</span
      >
    </div>
  </div>
</template>

<script>
import { starArticle } from '@/api/api.js'
export default {
  props: ['item', 'value'],
  data () {
    return {
      baseUrl: process.env.VUE_APP_URL,
      zannum: '',
      iszan: false
    }
  },
  created () {
    this.item.title = this.item.title.replace(
      this.value,
      `<span style="color:red">${this.value}</span>`
    )

    this.zannum = this.item.star
    if (this.zannum === 0) {
      this.iszan = true
    }
  },
  methods: {
    // 到文章详情
    toArt () {
      this.$router.push(`/shareDetail/${this.item.id}`)
    },
    // 点赞
    async zan (id) {
      this.iszan = !this.iszan
      const res = await starArticle({ article: id })
      this.zannum = res.data.data.num
    }
  }
}
</script>

<style lang="less" scoped>
* {
  padding: 0;
  margin: 0;
}
.shareArt {
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
  h3 {
    font-size: 15px;
    font-weight: normal;

    margin: 16px;
  }
  p {
    font-size: 14px;
    line-height: 16px;
    height: 32px;
    padding: 0px 15px 16px;
    display: -webkit-box;
    overflow: hidden;
    white-space: normal;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .info {
    display: flex;
    align-items: center;
    padding: 20px 15px 0;
    img {
      width: 26px;
      height: 26px;
      border-radius: 50%;
    }
    span {
      font-size: 12px;
      color: #999;
    }
    .uname {
      margin-right: 60px;
      margin-left: 14px;
    }
    .time,
    .read {
      margin-right: 16px;
    }
  }
  .red {
    color: red;
  }
}
</style>
